import http from '@/utils/http'

const BASE_URL = '/system/dict'

export type DictType = 'custom' | 'system'

export interface DictResp {
  id: string
  code: string
  name: string
  reserved: boolean
  description: string
  createTime: string
  updateTime: string
}

export interface DictNameResp {
  name: string
}

export interface DictDetailResp {
  id: string
  code: string
  name: string
  reserved: boolean
  description: string
  createUser: string
  createTime: string
  updateUser: string
  updateTime: string
}

export interface DictQuery extends SortQuery {
  id?: string
  code?: string
  name?: string
  reserved?: boolean
}

export interface DictPageQuery extends DictQuery, PageQuery {

}

export interface DictItemResp {
  id: string
  dictId: string
  label: string
  value: string
  color: string
  sort: string
  status: string
  description: string
}

export interface DictItemDetailResp {
  id: string
  dictId: string
  label: string
  value: string
  color: string
  sort: string
  status: number
  description: string
  createUser: string
  createTime: string
  updateUser: string
  updateTime: string
}

export interface DictItemQuery extends SortQuery {
  dictId?: string
  status?: string
}

export interface DictItemPageQuery extends DictItemQuery, PageQuery {

}

export function listDict(query: DictQuery) {
  return http.get<DictResp[]>(`${BASE_URL}/list`, query)
}

export function listEnumDict() {
  return http.get<DictResp[]>(`${BASE_URL}/list/enum`)
}

export function getDictIdByCode(code: string) {
  return http.get<string>(`${BASE_URL}/item/get-id-by-code/${code}`)
}

/** @desc 查询字典列表 */
export function getPage(query: DictPageQuery) {
  return http.get<Page<DictResp[]>>(`${BASE_URL}/page`, query)
}

/** @desc 查询字典详情 */
export function getDictDetail(id: string) {
  return http.get<DictDetailResp>(`${BASE_URL}/detail/${id}`)
}

/** @desc 创建字典 */
export function createDict(data: any) {
  return http.post(`${BASE_URL}/create`, data)
}

/** @desc 修改字典 */
export function updateDict(data: any, id: string) {
  return http.post(`${BASE_URL}/update/${id}`, data)
}

/** @desc 删除字典 */
export function removeDict(id: string) {
  return http.post(`${BASE_URL}/delete/${id}`)
}

// 字典项

/** @desc 查询字典项列表 */
export function listDictItem(query: DictItemQuery) {
  return http.get(`${BASE_URL}/item/list`, query)
}

/** @desc 查询字典项详情 */
export function getDictItemDetail(id: string) {
  return http.get<DictItemDetailResp>(`${BASE_URL}/item/detail/${id}`)
}

/** @desc 创建字典项 */
export function createDictItem(data: any) {
  return http.post(`${BASE_URL}/item/create`, data)
}

/** @desc 修改字典项 */
export function updateDictItem(data: any, id: string) {
  return http.post(`${BASE_URL}/item/update/${id}`, data)
}

/** @desc 删除字典项 */
export function removeDictItem(id: string) {
  return http.post(`${BASE_URL}/item/delete/${id}`)
}

/** @desc 排序字典项 */
export function sortDictItem(data: any) {
  return http.post(`${BASE_URL}/item/sort`, data)
}
